package com.songlanyun.modules.schedule;

import com.songlanyun.common.config.SemaphoreTool;
import com.songlanyun.common.enums.OrderConstant;
import com.songlanyun.common.utils.RedisUtils;
import com.songlanyun.modules.comment.model.vo.OrderGoodsCommentVO;
import com.songlanyun.modules.marketing.groupbuy.entity.GroupBuyTeam;
import com.songlanyun.modules.marketing.groupbuy.model.vo.GroupBuyActivityGoodsVO;
import com.songlanyun.modules.marketing.groupbuy.model.vo.GroupBuyActivityVO;
import com.songlanyun.modules.marketing.groupbuy.service.GroupBuyActivityGoodsService;
import com.songlanyun.modules.marketing.groupbuy.service.GroupBuyTeamService;
import com.songlanyun.modules.marketing.seckill.model.vo.SeckillActivityGoodsVO;
import com.songlanyun.modules.marketing.seckill.service.SeckillActivityGoodsService;
import com.songlanyun.modules.order.entity.ChildOrder;
import com.songlanyun.modules.order.entity.Order;
import com.songlanyun.modules.order.service.ChildOrderService;
import com.songlanyun.modules.order.service.OrderOperationService;
import com.songlanyun.modules.order.service.OrderService;
import com.songlanyun.modules.payorder.entity.PayOrder;
import com.songlanyun.modules.payorder.service.PayOrderService;
import com.songlanyun.modules.refillorder.service.SaleRefillCardOrderService;
import com.songlanyun.modules.rights.entity.RightsOrder;
import com.songlanyun.modules.rights.service.RightsOrderService;
import com.songlanyun.modules.saleconfig.entity.SaleConfig;
import com.songlanyun.modules.saleconfig.service.SaleConfigService;
import com.songlanyun.modules.ship.entity.OrderPackage;
import com.songlanyun.modules.ship.service.OrderPackageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 定时任务
 * <p>
 * 检测过期订单，修改状态
 */
@Component
@Configuration
@EnableScheduling
@Lazy(false)
@Slf4j
public class ActivityScheduleServiceImpl {

    @Resource
    private SeckillActivityGoodsService seckillActivityGoodsService;

    @Resource
    private GroupBuyActivityGoodsService groupBuyActivityGoodsService;

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private SemaphoreTool semaphoreTool;

    /**
     * 轮询在信号量中设置秒杀活动中的库存
     * 防止redis挂掉
     */
    @Async
    @Scheduled(fixedDelay = 60 * 1000)
    public void seckillTask() {
        //查询所有正在进行中的秒杀活动商品
        List<SeckillActivityGoodsVO> list = seckillActivityGoodsService.seckillingGoodsList(null);

        //遍历设置库存（信号量）
        for (SeckillActivityGoodsVO vo : list) {
            //如果剩余库存为0，则不设置
            if (vo.getResidueStockTotal() <= 0) {
                continue;
            }
            String key = SeckillActivityGoodsService.getSemaphoreKey(vo.getSeckillId(), vo.getId(), vo.getGoodsId());
            Integer num = redisUtils.get(key, Integer.class);
            //如果获取不到数量，则重新设置
            if (num == null || num <= 0) {
                semaphoreTool.setRSemaphorePermits(key, vo.getResidueStockTotal());
            }
        }

    }

    /**
     * 轮询在信号量中设置拼团活动中的库存
     * 防止redis挂掉
     */
    @Async
    @Scheduled(fixedDelay = 60 * 1000)
    public void groupbuyTask() {
        //查询所有正在进行中的拼团活动商品
        List<GroupBuyActivityGoodsVO> list = groupBuyActivityGoodsService.groupBuyingGoodsList(null);
        //遍历设置库存（信号量）
        for (GroupBuyActivityGoodsVO vo : list) {
            //如果剩余库存为0，则不设置
            if (vo.getResidueStockTotal() == 0) {
                continue;
            }
            String key = GroupBuyActivityGoodsService.getSemaphoreKey(vo.getGroupBuyId(), vo.getId(), vo.getGoodsId());
            Integer num = redisUtils.get(key, Integer.class);
            //如果获取不到数量，则重新设置
            if (num == null || num <= 0) {
                semaphoreTool.setRSemaphorePermits(key, vo.getResidueStockTotal());
            }

        }

    }
}